//@author A0106044W
package logic;

import java.util.ArrayList;

import commandInfo.CommandInfo;
import commandInfo.StatusFilterCommandInfo;
import storage.Task;

/**
 * 
 * 
 * 	       Sothearith Sreang
 *         All of the Logic part is written by me.
 * 
 */

public class PendingFilterHandler extends OperationHandler {

	ArrayList<Task> filterTasksList;
	ArrayList<Boolean> taskStatusList;

	private static final String SUCCESSFUL_PENDING_MESSAGE = "%d task%s are successfully set to pending";
	private static final String SUCCESSFUL_UNDO_MESSAGE = "%d task%s are now successfully set to their previous statuses";

	private static final String COMMAND_COMPLETE = "COMPLETE";
	private static final String COMMAND_PENDING = "PENDING";

	// Two public constructors

	// for normal pending operation
	public PendingFilterHandler(ArrayList<Task> taskList) {
		super();
		initializeList();
		filterTasksList = taskList;
	}

	// for undo, redo pending operation
	public PendingFilterHandler(CommandInfo commandInfoP) {
		super();
		initializeList();
		setUpTasksList(commandInfoP);
	}

	// This method is called for user command's pending operation
	
	public String execute() {
		for (Task task : filterTasksList) {
			taskStatusList.add(task.getDoneStatus());
			task.setPending();
		}

		super.commandInfo = new StatusFilterCommandInfo(COMMAND_PENDING,
				filterTasksList, taskStatusList);

		String feedback =  Helper.setUpFeedback(SUCCESSFUL_PENDING_MESSAGE, filterTasksList.size());
		return feedback;
	}
	
	// This method is called for undo for previous pending operation

	public String undo() {
		ArrayList<Boolean> statusList = new ArrayList<Boolean>();

		for (int index = 0; index < filterTasksList.size(); index++) {
			Task task = filterTasksList.get(index);
			boolean status = taskStatusList.get(index);

			task.setStatus(status);
			statusList.add(false);
		}

		super.commandInfo = new StatusFilterCommandInfo(COMMAND_COMPLETE,
				filterTasksList, statusList);

		String feedback = Helper.setUpFeedback(SUCCESSFUL_UNDO_MESSAGE, filterTasksList.size());
		return feedback;
	}

	// next level abstraction

	private void initializeList() {
		filterTasksList = new ArrayList<Task>();
		taskStatusList = new ArrayList<Boolean>();
	}
	
	private void setUpTasksList(CommandInfo commandInfoP) {
		ArrayList<Task> cloneTasksList = commandInfoP.getUpdatedTasksList();
		taskStatusList = commandInfoP.getStatusList();

		for (Task cloneTask : cloneTasksList) {
			Task task = super.getTask(cloneTask);
			assert task != null;
			filterTasksList.add(task);
		}

	}
}
